(function(vc) {
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addHousekeepingResourceInfo: {
                resId: '',
                resTypeId: '',
                resTypeName: '',
                resourceStoreTypes: [],
                resName: '',
                resCode: '',
                costPrice: '',
                price: '',
                stock: '',
                supplierId: '',
                supplierName: '',
                resourceSuppliers: [],
                remark: '',
                feeTypeCd: '',
                housekeepingFeeTypes: []
            }
        },
        _initMethod: function() {
           
        },
        _initEvent: function() {
            vc.on('addHousekeepingResource', 'openAddHousekeepingResourceModal', function() {
                $that._listAddResourceStoreTypes();
                $that._listAddResourceSuppliers();
                $that._queryAddHousekeepingFeeTypes();
                $('#addHousekeepingResourceModel').modal('show');
            });
        },
        methods: {
            addHousekeepingResourceValidate() {
                return vc.validate.validate({
                    addHousekeepingResourceInfo: $that.addHousekeepingResourceInfo
                }, {
                    'addHousekeepingResourceInfo.resTypeId': [{
                            limit: "required",
                            param: "",
                            errInfo: "物品类型ID不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "物品类型ID太长"
                        },
                    ],
                    'addHousekeepingResourceInfo.feeTypeCd': [{
                            limit: "required",
                            param: "",
                            errInfo: "费用类型名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "50",
                            errInfo: "物品类型名称太长"
                        },
                    ],
                    'addHousekeepingResourceInfo.resTypeName': [{
                            limit: "required",
                            param: "",
                            errInfo: "物品类型名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "物品类型名称太长"
                        },
                    ],
                    'addHousekeepingResourceInfo.resName': [{
                        limit: "maxLength",
                        param: "64",
                        errInfo: "物品名称太长"
                    }, ],
                    'addHousekeepingResourceInfo.resCode': [{
                        limit: "maxLength",
                        param: "30",
                        errInfo: "物品编码太长"
                    }, ],
                    'addHousekeepingResourceInfo.costPrice': [{
                            limit: "required",
                            param: "",
                            errInfo: "成本价不能为空"
                        },
                        {
                            limit: "money",
                            param: "",
                            errInfo: "成本价不是有效数字"
                        },
                    ],
                    'addHousekeepingResourceInfo.price': [{
                            limit: "required",
                            param: "",
                            errInfo: "出售价不能为空"
                        },
                        {
                            limit: "money",
                            param: "",
                            errInfo: "出售价不是有效数字"
                        },
                    ],
                    'addHousekeepingResourceInfo.stock': [{
                            limit: "required",
                            param: "",
                            errInfo: "库存不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "库存不是有效数字"
                        },
                    ],
                    'addHousekeepingResourceInfo.supplierId': [{
                            limit: "required",
                            param: "",
                            errInfo: "供应商ID不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "供应商ID太长"
                        },
                    ],
                    'addHousekeepingResourceInfo.supplierName': [{
                            limit: "required",
                            param: "",
                            errInfo: "供应商名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "供应商名称太长"
                        },
                    ],
                    'addHousekeepingResourceInfo.remark': [{
                        limit: "maxLength",
                        param: "512",
                        errInfo: "描述太长"
                    }, ]
                });
            },
            saveHousekeepingResourceInfo: function() {
                if (!$that.addHousekeepingResourceValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.addHousekeepingResourceInfo.shopId = vc.getCurrentCommunity().shopId;
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, $that.addHousekeepingResourceInfo);
                    $('#addHousekeepingResourceModel').modal('hide');
                    return;
                }
                vc.http.apiPost(
                    '/housekeepingResource/saveHousekeepingResource',
                    JSON.stringify($that.addHousekeepingResourceInfo), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addHousekeepingResourceModel').modal('hide');
                            $that.clearAddHousekeepingResourceInfo();
                            vc.emit('housekeepingResourceManage', 'listHousekeepingResource', {});
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            //查询方法
            _listAddResourceStoreTypes: function(_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送post请求
                vc.http.apiGet(
                    'resourceStoreType.listResourceStoreTypes',
                    param,
                    function(json, res) {
                        var _resourceStoreTypeManageInfo = JSON.parse(json);
                        $that.addHousekeepingResourceInfo.resourceStoreTypes = _resourceStoreTypeManageInfo.data;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listAddResourceSuppliers: function(_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet(
                    'resourceSupplier.listResourceSuppliers',
                    param,
                    function(json, res) {
                        var _resourceSupplierManageInfo = JSON.parse(json);
                        $that.addHousekeepingResourceInfo.resourceSuppliers = _resourceSupplierManageInfo.data;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _addSupplier: function() {
                $('#addHousekeepingResourceModel').modal('hide');
                vc.jumpToPage('/#/pages/goods/resourceSupplierManage?returnFlag=1');
            },
            setAddResTypeName: function(_resTypeId) {
                $that.addHousekeepingResourceInfo.resourceStoreTypes.forEach(item => {
                    if (item.rstId == _resTypeId) {
                        $that.addHousekeepingResourceInfo.resTypeName = item.name;
                    }
                });
            },
            setAddSupplierName: function(_supplierId) {
                $that.addHousekeepingResourceInfo.resourceSuppliers.forEach(item => {
                    if (item.rsId == _supplierId) {
                        $that.addHousekeepingResourceInfo.supplierName = item.supplierName;
                    }
                });
            },
            //查询方法
            _queryAddHousekeepingFeeTypes: function(_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/housekeepingFeeType/queryHousekeepingFeeType',
                    param,
                    function(json, res) {
                        var _housekeepingFeeTypeManageInfo = JSON.parse(json);
                        $that.addHousekeepingResourceInfo.housekeepingFeeTypes = _housekeepingFeeTypeManageInfo.data;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            clearAddHousekeepingResourceInfo: function() {
                $that.addHousekeepingResourceInfo = {
                    resTypeId: '',
                    resTypeName: '',
                    resourceStoreTypes: [],
                    resName: '',
                    resCode: '',
                    costPrice: '',
                    price: '',
                    stock: '',
                    supplierId: '',
                    supplierName: '',
                    resourceSuppliers: [],
                    remark: '',
                    feeTypeCd: '',
                    housekeepingFeeTypes: []
                };
            }
        }
    });
})(window.vc);